package com.xrkj.b2c.service.impl.message;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.xrkj.b2c.service.message.ShortMessageService;
import com.xrkj.b2c.utils.ShortMessageUtils;
import com.xrkj.b2c.utils.StringUtil;

/**
 * 
 * <B>系统名称：</B><BR>
 * <B>模块名称：</B><BR>
 * <B>中文类名：</B>短信服务类<BR>
 * <B>概要说明：</B><BR>
 * @author 赵子靖
 * @since 2018年07月15日
 */
@Service
public class ShortMessageServiceImpl implements ShortMessageService {
    private Logger logger=Logger.getLogger(ShortMessageServiceImpl.class);
    
    /**
     * 
     * <B>方法名称：</B>发送分享短消息<BR>
     * <B>概要说明：</B><BR>
     * @see com.hjl.oatalk.service.message.ShortMessageService#sendShareCompanyMessage(java.lang.String, java.lang.String)
     */
    @Override
    public Map<String, Object> sendShareCompanyMessage(String mobileStr, String shareContent) {
        Map<String, Object> ret = new HashMap<String, Object>();
        int sendSuccessCount = 0; //发送成功数量
        int sendErrorCount = 0;//发送失败数量
        int sendCount = 0;//总计发送数量
        if(StringUtil.isEmpty(mobileStr) || StringUtil.isEmpty(shareContent)){
           ret.put("success", false);
           ret.put("msg", "发送失败，手机号或内容为空。");
           return ret;
        }
        String[] mobiles = mobileStr.split(",");
        sendCount = mobiles.length;
        try{
            for(int i=0;i<mobiles.length;i++){
                if(ShortMessageUtils.sendMessageMethod(mobiles[i], shareContent)){
                    sendSuccessCount++;
                }else{
                    sendErrorCount++;
                }
            }
            ret.put("success", true);
            ret.put("msg", "发送成功，共发送"+sendCount+"条信息，其中成功："+sendSuccessCount+"条，失败："+sendErrorCount+"条。");
        }catch(IOException e){
            logger.error(e.getMessage());
            ret.put("success", false);
            ret.put("success", "发送失败，出现异常错误。");
        }
        return ret;
    }
}
